package com.unism.unisdk.core.transport.action.http;

import java.io.File;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import com.alibaba.fastjson.JSON;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.Response;
import com.ning.http.client.multipart.ByteArrayPart;
import com.ning.http.client.multipart.Part;
import com.unism.unisdk.common.enumObj.HttpMethod;
import com.unism.unisdk.core.transport.OpenResponse;
import com.unism.unisdk.core.transport.OpenResponseCallback;

public class HttpRequestHelper {
	public static OpenResponse httpRequestJson(final OpenResponseCallback callback, AsyncHttpClient asyncHttpClient,
			String requestId, int requestTimeout, String httpMethod, String url, Object requestJsonObject) {
		final OpenResponse openResponse = new OpenResponse();
		try {
			RequestBuilder requestBuilder = new RequestBuilder();
			requestBuilder.setUrl(url);
			requestBuilder.setRequestTimeout(requestTimeout);
			requestBuilder.setHeader("reqId", requestId);
			requestBuilder.setHeader("Content-Type", "application/json");

			if ("GET".equalsIgnoreCase(httpMethod)) {
				requestBuilder.setMethod("GET");
			} else if ("POST".equalsIgnoreCase(httpMethod)) {
				requestBuilder.setMethod("POST");
				requestBuilder.setBody(JSON.toJSONString(requestJsonObject));
				requestBuilder.setBodyEncoding("utf-8");
			} else {
				throw new Exception("http method只支持get、post");
			}

			Request request = requestBuilder.build();

			if (callback == null) {
				Response response = (Response) asyncHttpClient.executeRequest(request).get();
				openResponse.setHttpCode(response.getStatusCode());
				openResponse.setResult(response.getResponseBody("utf-8"));
			} else {
				asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Object>() {
					public void onThrowable(Throwable t) {
						openResponse.setThrowable(t);
						callback.onCallBack(openResponse);
					}
					public Void onCompleted(Response response) throws Exception {
						openResponse.setHttpCode(response.getStatusCode());
						openResponse.setResult(response.getResponseBody("utf-8"));
						callback.onCallBack(openResponse);
						return null;
					}
				});
			}
		} catch (Throwable e) {
			openResponse.setThrowable(e);
		}
		return openResponse;
	}
	public static void main(String[] args) {
		HttpRequestHelper.httpRequestJson(null, new AsyncHttpClient(),"1111111111", 0,"get","https://oapi.dingtalk.com/gettoken?corpid=ding9b5476b29d2f042035c2f4657eb6378f&corpsecret=tH0g0vhVG3R9wKeSZTUu0YzCw_Z28cOOQpevHBtYkyitnKZ0O1m854nZTRuH1mOO", args);
	}
	public static OpenResponse httpRequestFile(final OpenResponseCallback callback, AsyncHttpClient asyncHttpClient,
			String requestId, int requestTimeout, String url, String finalPostFileKey, File finalPostFile) {
		final OpenResponse openResponse = new OpenResponse();
		try {
			RequestBuilder requestBuilder = new RequestBuilder();
			requestBuilder.setUrl(url);
			requestBuilder.setRequestTimeout(requestTimeout);
			requestBuilder.setHeader("reqId", requestId);
			requestBuilder.setMethod("POST");

			Part part = new ByteArrayPart("media", FileUtils.readFileToByteArray(finalPostFile), "multipart/form-data",
					Charset.forName("utf-8"), finalPostFile.getName());
			requestBuilder.addBodyPart(part);

			Request request = requestBuilder.build();

			if (callback == null) {
				Response response = (Response) asyncHttpClient.executeRequest(request).get();
				openResponse.setHttpCode(response.getStatusCode());
				openResponse.setResult(response.getResponseBody("utf-8"));
			} else {
				asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Object>() {
					public void onThrowable(Throwable t) {
						openResponse.setThrowable(t);
						callback.onCallBack(openResponse);
					}
					public Void onCompleted(Response response) throws Exception {
						openResponse.setHttpCode(response.getStatusCode());
						openResponse.setResult(response.getResponseBody("utf-8"));
						callback.onCallBack(openResponse);
						return null;
					}
				});
			}
		} catch (Throwable e) {
			openResponse.setThrowable(e);
		}
		return openResponse;
	}
}